package org.nanotek.web.builder;

import java.util.Date;

import javax.validation.constraints.NotNull;

import org.nanotek.Builder;
import org.nanotek.base.person.Person;
import org.nanotek.base.person.date.PersonBirthDate;
import org.nanotek.base.person.date.PersonLifeCycle;
import org.nanotek.base.person.name.NamePersonType;
import org.nanotek.web.PersonBuilderException;


public class PersonBuilder implements Builder<Person>{

	private Person person = new Person();
	private Integer consistence = 0;
	
	public PersonBuilder()
	{ }
	
	public PersonBuilder setFirstName(@NotNull String value) {
//		person.getName().add(new NamePerson(NamePersonType.GivenName,value));
		person.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.GivenName).build());
		consistence = consistence | NamePersonType.GivenName.ordinal();
		return this;
	}
	
	public PersonBuilder setLastName(@NotNull String value) {
//		person.getName().add(new NamePerson(NamePersonType.LastName,value));
		person.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.LastName).build());
		consistence = consistence | NamePersonType.LastName.ordinal();
		return this;
	}
	
	public PersonBuilder setMiddleName(@NotNull String value) {
//		person.getName().add(new NamePerson(NamePersonType.MiddleName,value));
		person.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.MiddleName).build());
		return this;
	}

	public PersonBuilder setTitleName(@NotNull String value) {
//		person.getName().add(new NamePerson(NamePersonType.TitleName,value));
		person.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.TitleName).build());
		return this;
	}

	/**
	 * TODO: find the correct taxonomy defined when sons and grandsons have same same as the 
	 * father.
	 * 
	 * @param value
	 * @return
	 */
	public PersonBuilder setNomenClature(@NotNull String value) {
//		person.getName().add(new NamePerson(NamePersonType.Nomenclature,value));
		person.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.Nomenclature).build());
	return this;
	}

	/**
	 * Gmail Man: 
	 * Everything else
	 * **
	 * */
	public PersonBuilder setComplement(@NotNull String value) {
//		person.getName().add(new NamePerson(NamePersonType.Other,value));
		person.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.Other).build());
		return this;
	}

	public PersonBuilder setDateBirth(@NotNull Date dateBirth) {
		PersonBirthDate birthDate = new PersonBirthDate (dateBirth);
		if (person.getLifeCycle()==null)
				person.setLifeCycle(new PersonLifeCycle(birthDate));
		return this;
	}
	
	public Person build()
	{ 
		 checkConstraint();
		 return person;
	}
	
	protected void checkConstraint()
	{ 
		 int t = (consistence);
		 int ver = (NamePersonType.GivenName.ordinal() | NamePersonType.LastName.ordinal());
		 System.out.println(ver + "  " + t);
		 if (t < ver) 
			 throw new PersonBuilderException("InConsistent Record");
		 System.out.println(t);
	}
	
}
